package graphicalUserInterface;

import java.io.IOException;
import java.util.Calendar;

import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.DateTime;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.wb.swt.SWTResourceManager;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Image;
import org.jCharts.chartData.ChartDataException;
import org.jCharts.properties.PropertyException;
import org.xml.sax.SAXException;

import chart.BarChartExpense;
import chart.BarChartIncome;
import chart.ReportValues;
import exception.NullVariableFound;

import settings.MasterController;
//@author U096819H
public class StatisticsView extends Dialog {

	protected Object result;
	protected Shell shell;
	private String graphDir = "graphs";
	public StatisticsView(Shell parent, int style) {
		super(parent, style);
		setText("Report");
	}

	//@author U096819H
	public Object open(MasterController mc) {		
		createContents(mc, getParent().getDisplay());
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();		
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	//@author U096819H
	private void createContents(final MasterController mc, final Display display) {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(800, 600);
		shell.setText(getText());
		shell.setLayout(new FormLayout());
		
		ReportValues rv = new ReportValues();
		
		final Composite graphHolder = new Composite(shell, SWT.NONE);
		FormData fd_graphHolder = new FormData();
		fd_graphHolder.bottom = new FormAttachment(100, -10);
		fd_graphHolder.left = new FormAttachment(0, 10);
		fd_graphHolder.right = new FormAttachment(100, -10);
		graphHolder.setLayoutData(fd_graphHolder);
		
		Composite reportHolder = new Composite(shell, SWT.NONE);
		fd_graphHolder.top = new FormAttachment(0, 151);
		reportHolder.setLayout(new GridLayout(8, false));
		FormData fd_reportHolder = new FormData();
		fd_reportHolder.top = new FormAttachment(0, 10);
		fd_reportHolder.bottom = new FormAttachment(graphHolder, -6);
		fd_reportHolder.right = new FormAttachment(100, -10);
		reportHolder.setLayoutData(fd_reportHolder);
		
		Label lblTotalIncome = new Label(reportHolder, SWT.NONE);
		lblTotalIncome.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblTotalIncome.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
		lblTotalIncome.setText("Total Income:");
		
		Label totalInc = new Label(reportHolder, SWT.NONE);
		totalInc.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
		try {
			totalInc.setText(String.valueOf(mc.GetTotalIncome()));
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		new Label(reportHolder, SWT.NONE);
		
		Label lblMaxEarningCategory = new Label(reportHolder, SWT.NONE);
		lblMaxEarningCategory.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblMaxEarningCategory.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
		lblMaxEarningCategory.setText("Max Earning Category:");
		
		Label maxCatInc = new Label(reportHolder, SWT.NONE);
		maxCatInc.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));			
		try {
			maxCatInc.setText(rv.getMaxCatInc(mc));
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		new Label(reportHolder, SWT.NONE);
		
		Label lblMaxEarningSource = new Label(reportHolder, SWT.NONE);
		lblMaxEarningSource.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblMaxEarningSource.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
		lblMaxEarningSource.setText("Max Earning Source:");
		
		Label maxSrcInc = new Label(reportHolder, SWT.NONE);
		maxSrcInc.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
		try {
			maxSrcInc.setText(rv.getMaxSrcInc(mc));
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		Label lblTotalExpense = new Label(reportHolder, SWT.NONE);
		lblTotalExpense.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblTotalExpense.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
		lblTotalExpense.setText("Total Expense:");
		
		Label totalExp = new Label(reportHolder, SWT.NONE);
		totalExp.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
		try {
			totalExp.setText(String.valueOf(mc.GetTotalExpense()));
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		new Label(reportHolder, SWT.NONE);
		
		Label lblMinEarningCategory = new Label(reportHolder, SWT.NONE);
		lblMinEarningCategory.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblMinEarningCategory.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
		lblMinEarningCategory.setText("Min Earning Category:");
		
		Label minCatInc = new Label(reportHolder, SWT.NONE);
		minCatInc.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
		try {
			minCatInc.setText(rv.getMinCatInc(mc));
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		new Label(reportHolder, SWT.NONE);
		
		Label lblMinEarningSource = new Label(reportHolder, SWT.NONE);
		lblMinEarningSource.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblMinEarningSource.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
		lblMinEarningSource.setText("Min Earning Source:");
		
		Label minSrcInc = new Label(reportHolder, SWT.NONE);
		minSrcInc.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
		try {
			minSrcInc.setText(rv.getMinSrcInc(mc));
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		Label lblTotalSaving = new Label(reportHolder, SWT.NONE);
		lblTotalSaving.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblTotalSaving.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
		lblTotalSaving.setText("Total Saving:");
		
		Label totalSave = new Label(reportHolder, SWT.NONE);
		totalSave.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
		try {
			totalSave.setText(String.valueOf(mc.GetTotalBalance()));
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		new Label(reportHolder, SWT.NONE);
		
		Label lblMaxSpentCategory = new Label(reportHolder, SWT.NONE);
		lblMaxSpentCategory.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblMaxSpentCategory.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
		lblMaxSpentCategory.setText("Max Spent Category:");
		
		Label maxCatExp = new Label(reportHolder, SWT.NONE);
		maxCatExp.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
		try {
			maxCatExp.setText(rv.getMaxCatExp(mc));
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		new Label(reportHolder, SWT.NONE);
		
		Label lblMaxSpentSource = new Label(reportHolder, SWT.NONE);
		lblMaxSpentSource.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblMaxSpentSource.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
		lblMaxSpentSource.setText("Max Spent Source:");
		
		Label maxSrcExp = new Label(reportHolder, SWT.NONE);
		maxSrcExp.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
		try {
			maxSrcExp.setText(rv.getMaxSrcExp(mc));
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		Label lblTotalCategories = new Label(reportHolder, SWT.NONE);
		lblTotalCategories.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblTotalCategories.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
		lblTotalCategories.setText("Total Saving Pot:");
		
		Label totalCat = new Label(reportHolder, SWT.NONE);
		totalCat.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
		try {
			totalCat.setText(String.valueOf(mc.GetSavingPotAmount()));
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		new Label(reportHolder, SWT.NONE);
		
		Label lblMinSpentCategory = new Label(reportHolder, SWT.NONE);
		lblMinSpentCategory.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblMinSpentCategory.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
		lblMinSpentCategory.setText("Min Spent Category:");
		
		Label minCatExp = new Label(reportHolder, SWT.NONE);
		minCatExp.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
		try {
			minCatExp.setText(rv.getMinCatExp(mc));
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		new Label(reportHolder, SWT.NONE);
		
		Label lblMinSpentSource = new Label(reportHolder, SWT.NONE);
		lblMinSpentSource.setFont(SWTResourceManager.getFont("Segoe UI", 9, SWT.BOLD));
		lblMinSpentSource.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, true, true, 1, 1));
		lblMinSpentSource.setText("Min Spent Source:");
		
		Label minSrcExp = new Label(reportHolder, SWT.NONE);
		minSrcExp.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, true, true, 1, 1));
		try {
			minSrcExp.setText(rv.getMinSrcExp(mc));
		} catch (Exception e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		Label label_11 = new Label(reportHolder, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData gd_label_11 = new GridData(SWT.FILL, SWT.CENTER, true, false, 8, 1);
		gd_label_11.widthHint = 615;
		label_11.setLayoutData(gd_label_11);
		
		Composite menuHolder = new Composite(shell, SWT.NONE);
		fd_reportHolder.left = new FormAttachment(menuHolder, 12);
		menuHolder.setLayout(new GridLayout(2, false));
		FormData fd_menuHolder = new FormData();
		fd_menuHolder.bottom = new FormAttachment(graphHolder, -6);
		fd_menuHolder.left = new FormAttachment(0, 10);
		menuHolder.setLayoutData(fd_menuHolder);
		
		Label label_5 = new Label(menuHolder, SWT.NONE);
		label_5.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_5.setText("from");
		
		final DateTime dateFrom = new DateTime(menuHolder, SWT.BORDER | SWT.DROP_DOWN);
		
		Label label_12 = new Label(menuHolder, SWT.NONE);
		label_12.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		label_12.setText("to");
		
		final DateTime dateTo = new DateTime(menuHolder, SWT.BORDER | SWT.DROP_DOWN);
		
		Label label_13 = new Label(menuHolder, SWT.NONE);
		label_13.setText("type");
		
		Button radioExpense = new Button(menuHolder, SWT.RADIO);
		radioExpense.setText("Expense");
		new Label(menuHolder, SWT.NONE);
		System.out.println(radioExpense.toString());
		
		final Button radioIncome = new Button(menuHolder, SWT.RADIO);
		radioIncome.setText("Income");
		new Label(menuHolder, SWT.NONE);
		
		Button btnGo = new Button(menuHolder, SWT.NONE);
		btnGo.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				Calendar _dateFrom = Calendar.getInstance();
				_dateFrom.set(dateFrom.getYear(), dateFrom.getMonth(),
						dateFrom.getDay());
				Calendar _dateTo = Calendar.getInstance();
				_dateTo.set(dateTo.getYear(), dateTo.getMonth(),
						dateTo.getDay());
				if(_dateTo.after(_dateFrom)){
					System.out.println("---::: Rajul::" + radioIncome.getSelection());
					if(radioIncome.getSelection()){
						BarChartIncome bci = new BarChartIncome();
						try {
							bci.generateChart(mc, _dateFrom, _dateTo);
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}else{
						BarChartExpense bce = new BarChartExpense();
						try {
							bce.generateChart(mc, _dateFrom, _dateTo);
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
					}
					Image image = new Image(display, graphDir+"/barChart.png");
					graphHolder.setBackgroundImage(image);					
				}else{
					//prompt
				}
				
			}
		});
		GridData gd_btnGo = new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1);
		gd_btnGo.widthHint = 37;
		btnGo.setLayoutData(gd_btnGo);
		btnGo.setText("Go");

	}
}
